package model;

import java.util.Date;

/**
 * @author Alexander Shidlovsky
 */
public class TaskInfo {
    private Process process;
    private Date startTime;
    private Date finishTime;

    public TaskInfo(Process process) {
	this.process = process;
	startTime = null;
	finishTime = null;
    }

    public Process getProcess() {
	return process;
    }

    public void setProcess(Process process) {
	this.process = process;
    }

    public Date getStartTime() {
	return startTime;
    }

    public void setStartTime(Date startTime) {
	this.startTime = startTime;
    }

    public Date getFinishTime() {
	return finishTime;
    }

    public void setFinishTime(Date finishTime) {
	this.finishTime = finishTime;
    }

    public boolean isInLimits() {
	long running = this.calculateRunning();
	if (running < process.getMinTime() || running > process.getMaxTime())
	    return false;

	return true;
    }

    public long calculateRunning() {
	Date now = new Date();
	long running = now.getTime() - startTime.getTime();

	return running;
    }

    public double calculateDifferenceFromIdeal() {
	double dif = Math.pow(
		(process.getIdealTime() - this.calculateRunning()), 2);

	return dif;
    }

    public double calculateTimeLeft() {
	double dif = startTime.getTime() + process.getMaxTime()
		- System.currentTimeMillis();

	return dif;
    }

    // true is finished on time; false is not. the difference is measured in
    // seconds
    public boolean finishProcess() {
	finishTime = new Date(System.currentTimeMillis());
	return ((finishTime.getTime() - startTime.getTime()) <= process
		.getMaxTime())
		&& (finishTime.getTime() - startTime.getTime() >= process
			.getMinTime()) ? true : false;

    }
}
